//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
// $Id: DMXAnalysisManager.cc
// GEANT4 tag $Name:
//
// Author: Alex Howard (alexander.howard@cern.ch)
//
// History:
// -----------
// 16 Jan 2002 Alex Howard     Created
// 17 June 2002 Alex Howard    Successfully Modified to AIDA 2.2
// 17 November 2002 Alex Howard Migrated to AIDA 3.0 and added fitting
// 22 October 2009 Luciano Pandola Changed variables names for ntuple2
//                                 (avoid problem with columns) and some 
//                                 clean-up (e.g. remove ntuple4)
//
// -------------------------------------------------------------------
#ifdef  G4ANALYSIS_USE

#include "DMXAnalysisManager.hh"

DMXAnalysisManager* DMXAnalysisManager::instance = 0;

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

DMXAnalysisManager::DMXAnalysisManager() 
{;}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

DMXAnalysisManager::~DMXAnalysisManager() 
{;}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

DMXAnalysisManager* DMXAnalysisManager::getInstance()
{
  if (instance == 0) instance = new DMXAnalysisManager();
  return instance;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....


void DMXAnalysisManager::book(G4String histogramfile, G4bool)
{;}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void DMXAnalysisManager::Init()
{;}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void DMXAnalysisManager::Finish()
{
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
void DMXAnalysisManager::PulseTimeFit()
{
}


//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
void DMXAnalysisManager::analyseScintHits(G4int event_id, G4double energy_pri, G4double totEnergy, G4int S_hits, G4double firstLXeHitTime, G4int P_hits, G4double aveTimePmtHits, G4String firstparticleName, G4double firstParticleE, G4bool gamma_ev, G4bool neutron_ev, G4bool positron_ev,G4bool electron_ev,G4bool other_ev, long seed1, long seed2)

{

  G4int firstparticleIndex = 0;
  if(firstparticleName == "gamma") firstparticleIndex = 1;
  if(firstparticleName == "neutron") firstparticleIndex = 2;
  if(firstparticleName == "electron") firstparticleIndex = 3;
  if(firstparticleName == "positron") firstparticleIndex = 4;
  if(firstparticleName == "other") {
    firstparticleIndex = 5;
    hEdepRecoil->fill(totEnergy);
  }

  hNumPhLow->fill(P_hits,10.);  // fill(x,weight) 

  hNumPhHigh->fill(P_hits);  // fill(x,y,weight) 


  hEsourcep->fill( energy_pri/keV );

  hEdepp->fill( totEnergy/keV );
  
  hAvPhArrival->fill(aveTimePmtHits/ns);

 // Fill the ntuple
  ntuple2->fill( ntuple2->findColumn( "Event"   ), (G4float) event_id          );
  ntuple2->fill( ntuple2->findColumn( "e_prim"  ), (G4float) (energy_pri/keV)    );
  ntuple2->fill( ntuple2->findColumn( "tot_e"   ), (G4float) totEnergy         );
  ntuple2->fill( ntuple2->findColumn( "s_hits"  ), (G4float) S_hits            );
  ntuple2->fill( ntuple2->findColumn( "xe_time" ), (G4float) firstLXeHitTime   );
  ntuple2->fill( ntuple2->findColumn( "num_ph"  ), (G4float) P_hits            );
  ntuple2->fill( ntuple2->findColumn( "avphtime"), (G4float) aveTimePmtHits    );
  ntuple2->fill( ntuple2->findColumn( "firstpart" ), (G4float) firstparticleIndex);
  ntuple2->fill( ntuple2->findColumn( "firstparte"), (G4float) firstParticleE    );
  ntuple2->fill( ntuple2->findColumn( "gamma"   ), (G4float) gamma_ev          );
  ntuple2->fill( ntuple2->findColumn( "neutron" ), (G4float) neutron_ev        );
  ntuple2->fill( ntuple2->findColumn( "posi"    ), (G4float) positron_ev       );
  ntuple2->fill( ntuple2->findColumn( "elec"    ), (G4float) electron_ev       );
  ntuple2->fill( ntuple2->findColumn( "other"   ), (G4float) other_ev          );
  ntuple2->fill( ntuple2->findColumn( "seed1"   ), (G4float) seed1             );
  ntuple2->fill( ntuple2->findColumn( "seed2"   ), (G4float) seed2             );

  //Values of attributes are prepared; store them to the nTuple:
  ntuple2->addRow();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void DMXAnalysisManager::analysePMTHits(G4int event, G4int i, G4double x, G4double y, G4double z)
{

  hPMTHits->fill(x/mm, y/mm);  // fill(x,y,weight)     

  if (event == 0 ) {
    h1stPMTHit->fill(x,y);
  }

  // Fill the ntuple
  ntuple3->fill( ntuple3->findColumn( "event" ), (G4float) event );
  ntuple3->fill( ntuple3->findColumn( "hits"  ), (G4float) i     );
  ntuple3->fill( ntuple3->findColumn( "xpos"  ), (G4float) x     );
  ntuple3->fill( ntuple3->findColumn( "ypos"  ), (G4float) y     );
  ntuple3->fill( ntuple3->findColumn( "zpos"  ), (G4float) z     );

  // NEW: Values of attributes are prepared; store them to the nTuple:
  ntuple3->addRow(); 

}

void DMXAnalysisManager::analysePrimaryGenerator(G4double energy)
{

  // Fill energy ntple:
  ntuple1->fill( ntuple1->findColumn( "energy" ), energy );

  // NEW: Values of attributes are prepared; store them to the nTuple:
  ntuple1->addRow(); 

}

void DMXAnalysisManager::analyseParticleSource(G4double energy, G4String name)
{

  if(name == "gamma") {
    hGammaEdep->fill(energy/keV);
  }
  if(name == "neutron") {
    hNeutronEdep->fill(energy/keV);  // fill(x,weight)     
  }    
    if(name == "electron") {
      hElectronEdep->fill(energy/keV);  // fill(x,weight)     
  }    
    if(name == "positron") {
      hPositronEdep->fill(energy/keV);  // fill(x,weight)     
  }    
    if(name == "other") {
      hOtherEdep->fill(energy/keV);  // fill(x,weight)     
  }    

}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void DMXAnalysisManager::HistTime(G4double time)
{   
  hPhArrival->fill(time/ns);  // fill(x,y,weight)
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void DMXAnalysisManager::PlotHistosInter(G4int) 
{
  // // The plotter is updated only if there are some hits in the event
//   if(!flag) return;
//   //  Set the plotter ; set the number of regions and attach histograms
//   // to plot for each region.
//   //  It is done here, since then EndOfRun set regions
//   // for paper output.
//   if(plotter) {
//       plotter->currentRegion().plot(*hNumPhLow);
//       plotter->refresh();
//   }
}

void DMXAnalysisManager::PlotHistos(G4bool)
{   
  /*
  if(!plotter) plotter  = pf->create();
  plotter->show();
  plotter->setParameter("pageTitle","DMX Output Summary");

  //  G4int n = 1;
  if(plotter) {
    // We set one single region for the plotter
    // We now print the histograms, each one in a separate file
    plotter->createRegions(1,1);
    plotter->currentRegion().plot(*hNumPhHigh);
    plotter->refresh();
    plotter->writeToFile("NumberPhotons.ps","ps");
    
    plotter->createRegions(1,1);
    plotter->currentRegion().plot(*hEdepp);
    plotter->refresh();
    plotter->writeToFile("EnergyDeposit.ps","ps");

    plotter->createRegions(1,1);
    plotter->currentRegion().plot(*hEsourcep);
    plotter->refresh();
    plotter->writeToFile("SourceEnergy.ps","ps");
    
    plotter->createRegions(1,1);      
    plotter->currentRegion().plot(*hNumPhLow);
    plotter->refresh();
    plotter->writeToFile("NumberPhotonsLow.ps","ps");
  }
    //   // Creating two regions
    //   plotter->clearRegions();
    //   //  plotter->createRegions(2, 2, 0); // set the current working region to the first one
    //   plotter->show();
    
    if (interactive) {
      // Wait for the keyboard return to avoid destroying the plotter window too quickly.
      G4cout << "Press <ENTER> to exit" << G4endl;
      G4cin.get();
    }
  */
}

#endif
